/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.DocProject
{
	internal sealed partial class DataGridViewFileCellControl : UserControl, IDataGridViewEditingControl
	{
		#region Public Properties
		public string Value
		{
			get
			{
				return textBox.Text;
			}
			set
			{
				textBox.Text = value;
			}
		}

		public DataGridViewFileColumn Column
		{
			get
			{
				return column;
			}
			set
			{
				column = value;
			}
		}
		#endregion

		#region Private / Protected
		private DataGridViewFileColumn column;
		private DataGridView dataGridView;
		private bool valueChanged;
		private int rowIndex;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DataGridViewFileCellControl" /> class.
		/// </summary>
		public DataGridViewFileCellControl()
		{
			InitializeComponent();

			textBox.AutoSize = false;
		}
		#endregion

		#region Methods
		private void BrowseForFile()
		{
			using (OpenFileDialog dialog = new OpenFileDialog())
			{
				string path = PathFormat.ToPath(textBox.Text, false);

				if (path != null)
				{
					dialog.FileName = System.IO.Path.GetFileName(path);
					dialog.InitialDirectory = System.IO.Path.GetDirectoryName(path);
				}

				dialog.Title = fileToolStripMenuItem.Text;
				dialog.Filter = "All Files (*.*)|*.*";

				if (column != null)
					column.OnInitializeFileDialog(new DataGridViewFileColumnDialogEventArgs(column.DisplayIndex, dialog));

				if (dialog.ShowDialog(dataGridView.FindForm()) == DialogResult.OK)
					textBox.Text = dialog.FileName;
			}
		}

		private void BrowseForFolder()
		{
			using (FolderBrowserDialog dialog = new FolderBrowserDialog())
			{
				string path = PathFormat.ToPath(textBox.Text, false);

				if (path != null)
					dialog.SelectedPath = path;

				dialog.Description = folderToolStripMenuItem.Text;

				if (column != null)
					column.OnInitializeFolderDialog(new DataGridViewFileColumnFolderDialogEventArgs(column.DisplayIndex, dialog));

				if (dialog.ShowDialog(dataGridView.FindForm()) == DialogResult.OK)
					textBox.Text = PathFormat.EnsureDirectory(dialog.SelectedPath);
			}
		}
		#endregion

		#region Event Handlers
		private void textBox_TextChanged(object sender, EventArgs e)
		{
			valueChanged = true;
			dataGridView.NotifyCurrentCellDirty(true);
		}

		private void textBox_Enter(object sender, EventArgs e)
		{
			textBox.SelectAll();
		}

		private void fileToolStripMenuItem_Click(object sender, EventArgs e)
		{
			BrowseForFile();
		}

		private void folderToolStripMenuItem_Click(object sender, EventArgs e)
		{
			BrowseForFolder();
		}
		#endregion

		#region IDataGridViewEditingControl Members
		public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
		{
			if (dataGridViewCellStyle == null)
				throw new ArgumentNullException("dataGridViewCellStyle");

			textBox.Font = dataGridViewCellStyle.Font;
			textBox.ForeColor = dataGridViewCellStyle.ForeColor;
			textBox.BackColor = dataGridViewCellStyle.BackColor;

			browseToolStrip.Font = dataGridViewCellStyle.Font;
			browseToolStrip.ForeColor = dataGridViewCellStyle.ForeColor;
		}

		public DataGridView EditingControlDataGridView
		{
			get
			{
				return dataGridView;
			}
			set
			{
				dataGridView = value;
			}
		}

		public object EditingControlFormattedValue
		{
			get
			{
				return this.Value;
			}
			set
			{
				this.Value = (value == null) ? null : value.ToString();
			}
		}

		public int EditingControlRowIndex
		{
			get
			{
				return rowIndex;
			}
			set
			{
				rowIndex = value;
			}
		}

		public bool EditingControlValueChanged
		{
			get
			{
				return valueChanged;
			}
			set
			{
				valueChanged = value;
			}
		}

		public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
		{
			switch (keyData & Keys.KeyCode)
			{
				case Keys.Left:
				case Keys.Right:
				case Keys.Home:
				case Keys.End:
				case Keys.OemPeriod:
				case Keys.Decimal:
					return true;
				default:
					return false;
			}
		}

		public Cursor EditingPanelCursor
		{
			get
			{
				return base.Cursor;
			}
		}

		public bool RepositionEditingControlOnValueChange
		{
			get
			{
				return false;
			}
		}

		public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
		{
			return EditingControlFormattedValue;
		}

		public void PrepareEditingControlForEdit(bool selectAll)
		{
			bool canSelectFiles = column.CanSelectFiles;
			bool canSelectFolders = column.CanSelectFolders;

			if (canSelectFiles && canSelectFolders)
			{
				fileToolStripMenuItem.Visible = true;
				folderToolStripMenuItem.Visible = true;

				// user must choose from the two menu items - clicking the button simply drops down the items.
				browseToolStripDropDownButton.Click -= fileToolStripMenuItem_Click;
				browseToolStripDropDownButton.ShowDropDownArrow = true;
				browseToolStrip.Visible = true;
			}
			else
			{
				fileToolStripMenuItem.Visible = false;
				folderToolStripMenuItem.Visible = false;
				
				browseToolStripDropDownButton.ShowDropDownArrow = false;

				if (canSelectFiles)
					browseToolStripDropDownButton.Click += fileToolStripMenuItem_Click;
				else if (canSelectFolders)
					browseToolStripDropDownButton.Click += folderToolStripMenuItem_Click;
				else
					browseToolStrip.Visible = false;
			}

			if (selectAll)
				textBox.SelectAll();
		}
		#endregion
	}
}
